package com.alageek.crowd.service.impl;

import com.alageek.crowd.entity.Admin;
import com.alageek.crowd.entity.AdminExample;
import com.alageek.crowd.mapper.AdminMapper;
import com.alageek.crowd.service.api.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author alageek
 */
@Service
public class AdminServiceImpl implements AdminService {

    private final AdminMapper adminMapper;
    @Autowired
    public AdminServiceImpl(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    //private final Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);


    @Override
    public void update(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void remove(Integer adminId) {
        adminMapper.deleteByPrimaryKey(adminId);
    }

    @Override
    public PageInfo<Admin> getAdminPage(String keyword, Integer pageNum, Integer pageSize) {
        if(pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Admin> list = adminMapper.selectAdminListByKeyword(keyword);
        return new PageInfo<>(list);
    }

    @Override
    public List<Admin> getAdmin(Admin admin) {
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        if(admin.getLoginAcct() != null) {
            criteria.andLoginAcctEqualTo(admin.getLoginAcct());
        }
        if(admin.getUserPswd() != null) {
            criteria.andUserPswdEqualTo(admin.getUserPswd());
        }
        if(admin.getUserName() != null) {
            criteria.andUserNameEqualTo(admin.getUserName());
        }
        if(admin.getEmail() != null) {
            criteria.andEmailEqualTo(admin.getEmail());
        }
        return adminMapper.selectByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        adminMapper.insert(admin);
    }

    @Override
    public List<Admin> getAll() {
        return adminMapper.selectByExample(new AdminExample());
    }

    @Override
    public void saveAdminRoleRelationship(Integer adminId, List<Integer> roleIdList) {
        adminMapper.deleteOLdRelationship(adminId);
        if(roleIdList != null && roleIdList.size() > 0) {
            adminMapper.insertNewRelationship(adminId, roleIdList);
        }
    }

}
